package com.yudaob2b.elong.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.common.utils.ListUtil;
import com.yudaob2b.elong.constant.Constant;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.entity.dto.ELaElongHotelInvDTO;
import com.yudaob2b.elong.entity.vo.IncrInventory;
import com.yudaob2b.elong.entity.vo.Inventory;
import com.yudaob2b.elong.entity.vo.result.HotelDataInventoryResult;
import com.yudaob2b.elong.enums.ela.EnumElasticElongIndexName;
import com.yudaob2b.elong.service.HotelInventoryService;
import com.yudaob2b.elong.service.HttpStaticService;
import org.apache.commons.lang.StringUtils;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库存增量同步线程任务
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/20/18 8:42 PM
 */
public class IncrInvTask implements Runnable, Serializable {
    private static Logger incrInvLogger = LoggerFactory.getLogger("incrInvLogger");

    /**
     * 酒店id，最多10个
     */
    private String lastId;
    private ElongHotelInventoryService elongHotelInventoryService;
    private HotelInventoryService hotelInventoryService;
    private RedisTemplate redisTemplate;
    private ClientInterface clientUtil;

    public IncrInvTask(String lastId, ElongHotelInventoryService elongHotelInventoryService
            , HotelInventoryService hotelInventoryService, RedisTemplate redisTemplate
            , ClientInterface clientUtil) {
        this.lastId = lastId;
        this.elongHotelInventoryService = elongHotelInventoryService;
        this.hotelInventoryService = hotelInventoryService;
        this.redisTemplate = redisTemplate;
        this.clientUtil = clientUtil;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();

        if (StringUtils.isNotEmpty(lastId)) {
            Object inctInvListObj = redisTemplate.opsForValue().get(RedisConstant.REDIS_INCR_INV_L_KEY.replace("{lastid}", lastId));
            if (inctInvListObj != null) {
                List<IncrInventory> inctInvList = (List<IncrInventory>) inctInvListObj;
                // 待更新列表
                List<ELaElongHotelInvDTO> insertList = new ArrayList();
                for (IncrInventory incrInventory : inctInvList) {
                    insertList.add(hotelInventoryService.getElaDtoByInventory(incrInventory));
                }
                if (insertList.size() > 0) {
                    //elongHotelInventoryService.replaceInto(insertList);
                    clientUtil.addDocuments(EnumElasticElongIndexName.INVENTORY.getIndexName(),//索引表
                            EnumElasticElongIndexName.INVENTORY.getType(),//索引类型
                            insertList);
                }

                /*List<IncrInventory> inctInvList = (List<IncrInventory>) inctInvListObj;
                // 待更新列表
                List<ElongHotelInventoryPO> updateList = new ArrayList();
                // 缓存就酒店库存列表
                List<ElongHotelInventoryPO> oldElongHotelInvList = null;
                String hotelIdTemp = "";
                long start2 = System.currentTimeMillis();

                for (IncrInventory incrInventory : inctInvList) {
                    if (StringUtils.isEmpty(hotelIdTemp) || !hotelIdTemp.equals(incrInventory.getHotelID())) {
                        EntityWrapper<ElongHotelInventoryPO> ew = new EntityWrapper<>();
                        ew.where("hotel_id={0}", incrInventory.getHotelID());
                        oldElongHotelInvList = elongHotelInventoryService.selectList(ew);
                    }

                    // 检查当前ElongHotelInventoryPO是否已经存在
                    if (oldElongHotelInvList != null && oldElongHotelInvList.size() > 0) {
                        for (ElongHotelInventoryPO cacheInv : oldElongHotelInvList) {
                            if (cacheInv.getHotelId().equals(incrInventory.getHotelID())
                                    && cacheInv.getRoomTypeId().equals(incrInventory.getRoomTypeId())
                                    && cacheInv.getHotelCode().equals(incrInventory.getHotelCode())
                                    && cacheInv.getDate().compareTo(incrInventory.getDate()) == 0) {
                                updateList.add(hotelInventoryService.getPoByInventory(incrInventory));
                                break;
                            }
                        }
                    }
                }
                incrInvLogger.info("---处理耗时：---{}", System.currentTimeMillis() - start2);


                long start3 = System.currentTimeMillis();
                if (updateList.size() > 0) {
                    elongHotelInventoryService.updateBatch(updateList);
                }
                incrInvLogger.info("---入库耗时：---{}", System.currentTimeMillis() - start3);*/

            }
        }

        incrInvLogger.info("---库存增量线程总耗时：---{}", System.currentTimeMillis() - start);
    }
}
